package com.activenetwork.smlite.testcase.events.createEvents.selectComponents;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.Test;

import com.activenetwork.smlite.page.event.EventDetailView.EventBasicSubview;
import com.activenetwork.smlite.snippet.EventsSnippet;
import com.activenetwork.smlite.testcase.BaseCase;
import com.activenetwork.util.EnvContext;
import com.activenetwork.util.verification.gson.FailureAssertion;
import com.activenetwork.util.verification.gson.FailurePoint;

public class SelectComponents001 extends BaseCase {
	static final Logger logger = LoggerFactory.getLogger(SelectComponents001.class);
	
	private void checkNavigationHide(WebElement link, String linkName){
		if(!link.getAttribute("class").contains("hide")){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					SelectComponents001.class.getName(), 
					"Verify navigation link display", 
					linkName + " link is not hide by default"));
		}
		logger.info("Left navigation " + linkName + " check hide");
	}
	
	private void checkNavigationDisplay(WebElement link, String linkName){
		if(link.getAttribute("class").contains("hide")){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					SelectComponents001.class.getName(), 
					"Verify navigation link display", 
					linkName + " link is not display by default"));
		}
		logger.info("Left navigation " + linkName + " check display");
	}
	
	private void checkNaivgationGroup(WebElement link, String expectedGroup, String linkName){
		WebElement div = link.findElement(
				By.xpath("./ancestor::div[@class='event-feature-group']//div[@class='event-feature-group-heading btn']"));
		String group = div.getText().trim();
		if(!StringUtils.equalsIgnoreCase(group, expectedGroup)){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					SelectComponents001.class.getName(), 
					"Verify navigation link group", 
					linkName + " link group is not " + expectedGroup + " but " + group));
		}
		logger.info("Left navigation " + linkName + " check group");
	}
	
	private void checkDefaultReadOnly(WebElement checkbox, String componentName){
		String checked = checkbox.getAttribute("checked");
		if(!StringUtils.equalsIgnoreCase(checked, "true")){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					SelectComponents003.class.getName(), 
					"Verify checkbox default checked", 
					componentName + " is not checked by default"));
		}
		String readonly = checkbox.getAttribute("readonly");
		if(!StringUtils.equalsIgnoreCase(readonly, "true")){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					SelectComponents003.class.getName(), 
					"Verify checkbox default read-only", 
					componentName + " is not read-only"));
		}
		logger.info("Component checkbox " + componentName + " check readonly and selected");
	}
	
	private void checkDefaultReadOnlyNot(WebElement checkbox, String componentName){
		String checked = checkbox.getAttribute("checked");
		if(StringUtils.equalsIgnoreCase(checked, "true")){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					SelectComponents003.class.getName(), 
					"Verify checkbox default checked", 
					componentName + " is checked by default"));
		}
		String readonly = checkbox.getAttribute("readonly");
		if(StringUtils.equalsIgnoreCase(readonly, "true")){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					SelectComponents003.class.getName(), 
					"Verify checkbox default read-only", 
					componentName + " is read-only"));
		}
		logger.info("Component checkbox " + componentName + " check writable and unselected");
	}
	
	private void checkComponentGroup(WebElement p, String expectGroup, String componentName){
		String group = p.findElement(By.xpath("./parent::div[1]/p[1]")).getText().trim();
		if(!StringUtils.equalsIgnoreCase(group, expectGroup)){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					SelectComponents003.class.getName(), 
					"Verify checkbox group name", 
					componentName + " group is not " + expectGroup + " but " + group));
		}
		logger.info("Component checkbox " + componentName + " check group");
	}
	
	@Test
	public void Verify_UI_of_Event_Basics_Select_Events_Components_section(){
		loginBusiness.login();
		
		EventBasicSubview eventBasicSubview = EventsSnippet.gotoAddEventPage();
		WebDriver driver = EnvContext.webDriverTL.get();
		
		// Verify left navigation links default displaying
		// Default display : Registration Form, Theme Designer, Reports
		// Others are hidden
		 
		List<WebElement> links = driver.findElements(By.cssSelector(".event-feature-group div[id^='collapse-'] a[data-module]"));
		
		for(WebElement link : links){			
			
			// Only the displayed name can be seen
			if(!link.isDisplayed()){
				continue;
			}
			
			String linkName = link.getText().trim();	
				
			if(linkName.equals("Sourcing")){
				checkNavigationHide(link, linkName);
				checkNaivgationGroup(link, "Planning", linkName);
			}else if(linkName.equals("Social Media")){
				checkNavigationHide(link, linkName);
				checkNaivgationGroup(link, "Promote", linkName);
			}else if(linkName.equals("Website")){
				checkNavigationHide(link, linkName);
				checkNaivgationGroup(link, "Promote", linkName);
			}else if(linkName.equals("Email Invitations")){
				checkNavigationHide(link, linkName);
				checkNaivgationGroup(link, "Promote", linkName);
			}else if(linkName.equals("Attendee Directories")){
				checkNavigationHide(link, linkName);
				checkNaivgationGroup(link, "Promote", linkName);
			}else if(linkName.equals("Registration Form")){
				checkNavigationDisplay(link, linkName);
				checkNaivgationGroup(link, "Registration", linkName);
			}else if(linkName.equals("Theme Designer")){
				checkNavigationDisplay(link, linkName);
				checkNaivgationGroup(link, "Registration", linkName);
			}else if(linkName.equals("Mobile")){
				checkNavigationHide(link, linkName);
				checkNaivgationGroup(link, "On-site", linkName);
			}else if(linkName.equals("Badges & Labels")){
				checkNavigationHide(link, linkName);
				checkNaivgationGroup(link, "On-site", linkName);
			}else if(linkName.equals("Kiosk")){
				checkNavigationHide(link, linkName);
				checkNaivgationGroup(link, "On-site", linkName);
			}else if(linkName.equals("Reports")){
				checkNavigationDisplay(link, linkName);
				checkNaivgationGroup(link, "Analyze", linkName);
			}else if(linkName.equals("Custom Reports")){
				checkNavigationHide(link, linkName);
				checkNaivgationGroup(link, "Analyze", linkName);
			}else if(linkName.equals("Surveys")){
				checkNavigationHide(link, linkName);
				checkNaivgationGroup(link, "Analyze", linkName);
			}else{
				logger.error(linkName + " can not be checked");
				EnvContext.failurePointTL.get().add(new FailurePoint(
						SelectComponents001.class.getName(), 
						"Verify navigation link group", 
						linkName + " is unknown for checking"));
			}
		}
		
		
		// Check the UI for Event Setting Section
		try{
			WebElement componentHeader = driver.findElement(By.cssSelector(".autotest-html-eventbasicsubview .page-header p"));
			logger.info("Select component header is: " + componentHeader.getText());
		}catch(NoSuchElementException e){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					SelectComponents001.class.getName(), 
					"Verify select component section", 
					"Unable to find select component section"));
		}
		
		List<WebElement> componentParts = driver.findElements(By.cssSelector(".autotest-html-eventbasicsubview .row-fluid .span4"));
		try{
			componentParts.get(2).findElement(By.xpath(".//div[@id='venueSourcing']"));
		}catch(Exception e){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					SelectComponents001.class.getName(), 
					"Verify select component section", 
					"Unable to find component introduction part"));
		}
		
		// Verify checkbox components group and component name
		// Verify each checkbox default check on and read-only
		List<WebElement> mouseOvers = driver.findElements(By.cssSelector(".autotest-html-eventbasicsubview p.mouseoverClass"));
		for(WebElement mouseover : mouseOvers){
			String componentName = mouseover.getText().trim();
			if(componentName.equals("Sourcing")){
				checkDefaultReadOnlyNot(mouseover.findElement(By.xpath("./input")), componentName);
				checkComponentGroup(mouseover, "Plan", componentName);
			}else if(componentName.equals("Social Media")){
				checkDefaultReadOnlyNot(mouseover.findElement(By.xpath("./input")), componentName);
				checkComponentGroup(mouseover, "Promote", componentName);
			}else if(componentName.equals("Website")){
				checkDefaultReadOnlyNot(mouseover.findElement(By.xpath("./input")), componentName);
				checkComponentGroup(mouseover, "Promote", componentName);
			}else if(componentName.equals("Email Invitations")){
				checkDefaultReadOnlyNot(mouseover.findElement(By.xpath("./input")), componentName);
				checkComponentGroup(mouseover, "Promote", componentName);
			}else if(componentName.equals("Attendee Directories")){
				checkDefaultReadOnlyNot(mouseover.findElement(By.xpath("./input")), componentName);
				checkComponentGroup(mouseover, "Promote", componentName);
			}else if(componentName.equals("Registration Form")){
				checkDefaultReadOnly(mouseover.findElement(By.xpath("./input")), componentName);
				checkComponentGroup(mouseover, "Registration", componentName);
			}else if(componentName.equals("Theme Designer")){
				checkDefaultReadOnly(mouseover.findElement(By.xpath("./input")), componentName);
				checkComponentGroup(mouseover, "Registration", componentName);
			}else if(componentName.equals("Mobile")){
				checkDefaultReadOnlyNot(mouseover.findElement(By.xpath("./input")), componentName);
				checkComponentGroup(mouseover, "On-Site", componentName);
			}else if(componentName.equals("Badges & Labels")){
				checkDefaultReadOnlyNot(mouseover.findElement(By.xpath("./input")), componentName);
				checkComponentGroup(mouseover, "On-Site", componentName);
			}else if(componentName.equals("Kiosk")){
				checkDefaultReadOnlyNot(mouseover.findElement(By.xpath("./input")), componentName);
				checkComponentGroup(mouseover, "On-Site", componentName);
			}else if(componentName.equals("Reports")){
				checkDefaultReadOnly(mouseover.findElement(By.xpath("./input")), componentName);
				checkComponentGroup(mouseover, "Analyze", componentName);
			}else if(componentName.equals("Custom Reports")){
				checkDefaultReadOnlyNot(mouseover.findElement(By.xpath("./input")), componentName);
				checkComponentGroup(mouseover, "Analyze", componentName);
			}else if(componentName.equals("Surveys")){
				checkDefaultReadOnlyNot(mouseover.findElement(By.xpath("./input")), componentName);
				checkComponentGroup(mouseover, "Analyze", componentName);
			}else{
				EnvContext.failurePointTL.get().add(new FailurePoint(
						SelectComponents001.class.getName(), 
						"Verify component name and group", 
						componentName + " is unkown for checking"));
			}
		}
		
		eventBasicSubview = eventBasicSubview.showEventTag();
		By by = By.xpath("//div[@class='control-group ae-tagref-lookup autotest-lookup-EventBasicSubview-event-tagsref']//input/ancestor::span[contains(@class,'nexus-section-body')]");
		WebElement span = driver.findElement(by);
		String status = span.getAttribute("class").trim();
		if(StringUtils.containsIgnoreCase(status, "hide")){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					SelectComponents001.class.getName(), 
					"Verify show tag", 
					"Click advance but tag does not display"));
		}
		
		loginBusiness.logout();
		FailureAssertion.pop(SelectComponents001.class);
		
	}
}
